package com.amorcloud.pig.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v4.content.ContextCompat;
import android.text.Layout;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

import com.amorcloud.pig.R;
import com.amorcloud.pig.activity.ModifyPWDActivity;
import com.amorcloud.pig.bean.UserInfoBean;
import com.amorcloud.pig.utils.UserDaoUtils;
import com.amorcloud.pig.widget.CustomDialog;

import java.util.List;

public class UsersAdapter extends BaseAdapter {

    List<UserInfoBean> list;
    LayoutInflater mInflater;
    Context context;

    public UsersAdapter(Context context, List<UserInfoBean> users) {
        list = users;
        this.context = context;
        mInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHodler hodler;
        if( convertView == null) {
            convertView = mInflater.inflate(R.layout.item_user, parent, false);
            hodler = new ViewHodler();
            hodler.userName = convertView.findViewById(R.id.tv_username);
            hodler.modifyPwd = convertView.findViewById(R.id.btn_modify_pwd);
            hodler.deleteUser = convertView.findViewById(R.id.btn_delete_user);
            convertView.setTag(hodler);
        } else {
            hodler = (ViewHodler) convertView.getTag();
        }
        UserInfoBean bean = list.get(position);
        Log.d("leihuiming", "user=" + bean.getUserName() + " pwd=" + bean.getPasswrod() + " isadmin=" + bean.getIsAdmin());
        hodler.userName.setText(bean.getUserName());
        hodler.modifyPwd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, ModifyPWDActivity.class);
                intent.putExtra("title", "密码修改");
                intent.putExtra("userid", list.get(position).getId());
                context.startActivity(intent);

            }
        });
        hodler.deleteUser.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                String msg = String.format(context.getString(R.string.delete_user), list.get(position).getUserName());
                final CustomDialog dialog = new CustomDialog(context, R.style.MyDialog);
                dialog.setMessage(msg);
                dialog.setYesOnclickListener("确定", new CustomDialog.onYesOnclickListener() {
                    @Override
                    public void onYesOnclick() {
                        UserDaoUtils.deleteById(list.get(position).getId());
                        list.remove(position);
                        notifyDataSetChanged();
                        dialog.dismiss();
                    }
                });
                dialog.setNoOnclickListener("取消", new CustomDialog.onNoOnclickListener() {
                    @Override
                    public void onNoClick() {
                        dialog.dismiss();
                    }
                });
                dialog.show();
                final WindowManager.LayoutParams params = dialog.getWindow().getAttributes();
                params.width = 800;
                params.height = 660;
                dialog.getWindow().setAttributes(params);
            }
        });
        if("1".equals(bean.getIsAdmin())) {
            hodler.deleteUser.setVisibility(View.INVISIBLE);
        }
        return convertView;
    }

    public void setData(List<UserInfoBean> users) {
        this.list = users;
    }

    private class ViewHodler {
        TextView userName;
        Button modifyPwd, deleteUser;
    }
}
